package com.xoa.service.document.Impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.document.DocumentExchangeOutUnitMapper;
import com.xoa.model.document.DocumentExchangeOutUnit;
import com.xoa.service.document.DocumentExchangeOutUnitService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DocumentExchangeOutUnitServiceImpl implements DocumentExchangeOutUnitService {
    @Resource
    DocumentExchangeOutUnitMapper documentExchangeOutUnitMapper;
    @Resource
    private SysCodeMapper sysCodeMapper;

    //新建
    @Override
    public ToJson insert(DocumentExchangeOutUnit documentExchangeOutUnit) {
        ToJson json = new ToJson();
        int insert = documentExchangeOutUnitMapper.insert(documentExchangeOutUnit);
        if (insert > 0) {
            json.setFlag(0);
        } else {
            json.setFlag(1);
        }
        return json;
    }

    //删除
    @Override
    public ToJson delete(Integer ouId) {
        ToJson json = new ToJson();
        int i = documentExchangeOutUnitMapper.deleteByPrimaryKey(ouId);
        if (i > 0) {
            json.setFlag(0);
        } else {
            json.setFlag(1);
        }
        return json;
    }

    //更新
    @Override
    public ToJson update(DocumentExchangeOutUnit documentExchangeOutUnit) {
        ToJson json = new ToJson();
        int i = documentExchangeOutUnitMapper.updateByPrimaryKeySelective(documentExchangeOutUnit);
        if (i > 0) {
            json.setFlag(0);
        } else {
            json.setFlag(1);
        }
        return json;
    }

    //查询所有
    @Override
    public ToJson<DocumentExchangeOutUnit> query(Map map) {
        ToJson<DocumentExchangeOutUnit> json = new ToJson();
        PageParams page = (PageParams) map.get("page");
        List<DocumentExchangeOutUnit> query = documentExchangeOutUnitMapper.query(map);
        for (DocumentExchangeOutUnit doc : query) {
            doc.setOuTypeName(sysCodeMapper.getSingleCode("DOCUMENT_EXCHANGE_OUTUNIT", doc.getOuType()).getCodeName());
        }
        json.setTotleNum(page.getTotal());
        json.setFlag(0);
        json.setMsg("ok");
        json.setObj(query);
        return json;
    }

    //根据id查询一条数据
    public DocumentExchangeOutUnit selectById(Integer ouId) {
        return documentExchangeOutUnitMapper.selectByPrimaryKey(ouId);
    }
}
